package com.cloudpark.common.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;



public class JsonUtil {
	
	private static final Logger log = Logger.getLogger(JsonUtil.class);
	
	private static String ERROR_RESULT = "{\"code\":-1}";
	
	public static String toJSONDataTables(Pagination page, int draw) {
        try {
        	Map<String, Object> map = new HashMap<String, Object>();
            map.put("draw", draw);
            map.put("recordsTotal", page.getTotalRows());
            map.put("recordsFiltered", page.getTotalRows());
            map.put("data", page.getResultList());
            return JSON.toJSONStringWithDateFormat(map, DateUtils.YMDHMS);
        } catch (Exception e) {
            log.error("json转换失败", e);
        }
        return ERROR_RESULT;
    }

	public static String success(String text) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 1);
		map.put("text", text);
		return JSON.toJSONString(map);
	}
	
	public static String error(String text) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", -1);
		map.put("text", text);
		return JSON.toJSONString(map);
	}
}
